﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Vowei.Data.Converters.Excel;
using Vowei.Core.Models;
using System.IO;
using NPOI.HSSF.UserModel;
using Vowei.Core;
using System.Diagnostics;

namespace Vowei.Converters.Excel
{
    public class ResourceExcelConverter : ExcelConverter<IEnumerable<Resource>>
    {
        public ResourceExcelConverter() : base(DefaultTranslator) { } 

        public ResourceExcelConverter(ITranslator translator) : base(translator) { }

        public override bool CanConvert(System.IO.Stream stream)
        {
            throw new NotSupportedException(Translator.T("ResourceExcelConverter应该单独调用，请不要将它放在一个ExcelConverter数组里，通过遍历数组的方式调用！因为导入包含资源定义的Excel文件只有名称这个关键列！"));
        }

        public override IEnumerable<Resource> ConvertFrom(System.IO.Stream stream)
        {
            if (stream == null)
                throw new ArgumentNullException("stream");
            if (Translator == null)
                throw new InvalidOperationException(DefaultTranslator.T("在从Excel导入数据之前，请指定用户转换日期、数字、货币等格式的区域设置！"));

            var memoryStream = new MemoryStream((int)stream.Length);
            stream.CopyTo(memoryStream);
            var workbook = new HSSFWorkbook(memoryStream);
            var sheet = GetWorkItemsSheet(workbook);
            var firstRow = GuessFirstRow(sheet, Translator.T<Resource, string>(m => m.Title));

            var map = ReadColumnNames(sheet.GetRow(firstRow), new string[] {
                Translator.T<Resource, string>(m => m.Title), // "名称"
            },
            0, 256);
            var columnNames = ReadColumnNames(sheet.GetRow(firstRow), 0, 256);
            columnNames.Remove(Translator.T<Resource, string>(m => m.Title));

            var result = new List<Resource>();
            for (int i = (firstRow + 1); i < sheet.LastRowNum + 1; ++i)
            {
                var row = sheet.GetRow(i);
                var dic = new Dictionary<string, string>();

                try
                {
                    var title = ReadCell<string>(row, map[Translator.T<Resource, string>(m => m.Title)], true);

                    foreach (var name in columnNames)
                        dic.Add(name.Key, ReadCell<string>(row, name.Value, false));

                    var resource = new Resource();
                    resource.Title = title;
                    ExcelRowSerializer.Serialize(dic, resource);
                    result.Add(resource);
                }
                catch (Exception e)
                {
                    Trace.WriteLine(string.Format(Translator.T("在试图导入Excel文件的“{0}”工作表失败，第{1}行有误, 具体错误信息是：{2}"), WorkSheetName, i + 1, e.Message));
                }
            }

            return result;
        }

        public override void ConvertTo(IEnumerable<Resource> from, System.IO.Stream to)
        {
            throw new NotImplementedException();
        }
    }
}
